package com.rzl.expense.quartz.task;


import com.rzl.expense.agent.DataAgentService;
import com.rzl.expense.expense.domain.ElePaymentdetail;
import com.rzl.expense.expense.domain.EleVerification;
import com.rzl.expense.expense.domain.EleVerificationdetail;
import com.rzl.expense.expense.service.IElePaymentdetailService;
import com.rzl.expense.expense.service.IEleVerificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 电费核销单
 *
 * @author admin
 * @date 2023/08/09
 */
@Slf4j
@Component("EleVerificationTask")
public class EleVerificationTask {
    @Autowired
    private DataAgentService dataAgentService;

    @Autowired
    private IEleVerificationService eleVerificationService;


    /**
     * 集团用户同步
     */
    public void run() {

        log.info("获取集团用户数据..........");
        // 执行sql
        int size;
        // limit起始位置
        int startIndex = 0;
        // limit大小
        int limit = 20000;
        eleVerificationService.deleteEleVerification();
        do {

            List<EleVerification> list = (List<EleVerification>) dataAgentService.getData(RuleSqlAdapter.getEleVerification(startIndex, limit), EleVerification.class);
            if (!Objects.isNull(list) && list.size() > 0) {
                int insertSize = list.size();
                // limit起始位置
                int insertIndex = 0;
                // limit大小
                do {
                    List<EleVerification> eleVerifications = list.stream().skip(insertIndex).limit(1000).collect(Collectors.toList());
                    try {
                        if (!Objects.isNull(eleVerifications) && eleVerifications.size() > 0) {
                            eleVerificationService.batchEleVerification(eleVerifications);
                            insertSize = eleVerifications.size();
                            insertIndex = insertIndex + 1000;
                        } else {
                            insertSize = 0;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } while (insertSize >= 1000);
            }
            size = list.size();
            startIndex = startIndex + limit;
        } while (size >= limit);

        log.info("数据操作成功");
    }
}
